import uuid
from django.db import models
from django.utils import timezone

from ..AssetsManagement.models import Asset
from ..CanvasesManagement.models import Canvas
from ..FontManagement.models import Font


class CanvasElement(models.Model):
    """画布元素模型"""
    objects = models.Manager()

    ELEMENT_TYPE_CHOICES = [
        ('mask', 'Mask'),
        ('text', 'Text'),
        ('group', 'Group'),
    ]

    id = models.CharField(primary_key=True, default=uuid.uuid4, max_length=255, editable=False,
                          verbose_name="元素唯一标识")
    canvas = models.ForeignKey(Canvas, on_delete=models.CASCADE, related_name="elements", verbose_name="关联画布")
    type = models.CharField(max_length=10, choices=ELEMENT_TYPE_CHOICES, verbose_name="元素类型")
    name = models.CharField(max_length=100, verbose_name="元素名称")
    position = models.JSONField(default=dict, verbose_name="位置信息", help_text="格式：{'x': value, 'y': value}")
    size = models.JSONField(default=dict, verbose_name="尺寸信息", help_text="格式：{'width': value, 'height': value}")
    rotation = models.FloatField(default=0.0, verbose_name="旋转角度")
    opacity = models.FloatField(default=1.0, verbose_name="透明度 (0-1)")
    visible = models.BooleanField(default=True, verbose_name="是否可见")
    locked = models.BooleanField(default=False, verbose_name="是否锁定")
    frame_style = models.JSONField(default=dict, verbose_name="边框样式",
                                   help_text="格式：{border: '5px solid', borderColor: '#4a90e2', borderRadius: '15%'}")
    z_index = models.IntegerField(default=0, verbose_name="Z轴排序值（元素图层，0为最下层）")
    group_parent_id = models.CharField(max_length=255, null=True, blank=True, verbose_name="父group_id")
    created_at = models.DateTimeField(default=timezone.now, db_index=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        db_table = 'canvas_elements'
        verbose_name = '画布元素'
        verbose_name_plural = '画布元素'
        ordering = ['z_index']


class TextElement(models.Model):
    """文本元素扩展表"""
    objects = models.Manager()

    element = models.OneToOneField(CanvasElement, on_delete=models.CASCADE, primary_key=True,
                                   related_name="text_element", verbose_name="关联元素")
    text_content = models.TextField(verbose_name="文本内容", default="默认文本")
    font_family = models.CharField(max_length=500, default="Arial", verbose_name="字体名称")
    font_size = models.CharField(default="12.0", max_length=20, verbose_name="字体大小")
    font_weight = models.CharField(max_length=20, default="normal", verbose_name="字体粗细")
    font_style = models.CharField(max_length=20, default="normal", verbose_name="字体样式")
    text_align = models.CharField(max_length=20, default="left", verbose_name="对齐方式")
    line_height = models.CharField(default="1.2", max_length=20, verbose_name="行高")
    letter_spacing = models.CharField(default="0.0", max_length=20, verbose_name="字间距")
    text_color = models.CharField(max_length=20, default="#000000", verbose_name="文字颜色")
    text_effects = models.JSONField(default=dict, verbose_name="文字特效{shadow, glow, 3d}")
    text_transform = models.JSONField(default=dict, verbose_name="文字变形{path, arc}")
    writing_mode = models.CharField(max_length=20, default="horizontal", verbose_name="书写模式")
    font = models.ForeignKey(Font, on_delete=models.SET_NULL, null=True, blank=True, related_name="text_elements",
                             verbose_name="字体")

    class Meta:
        db_table = "text_elements"
        verbose_name = "文本元素"
        verbose_name_plural = "文本元素"


class MaskElement(models.Model):
    """蒙版元素扩展表"""
    objects = models.Manager()

    MASK_TYPE_CHOICES = [
        ("shape", "形状蒙版"),
        ("image", "图片蒙版"),
    ]

    element = models.OneToOneField(CanvasElement, on_delete=models.CASCADE, primary_key=True,
                                   related_name="mask_element", verbose_name="关联元素")
    mask_type = models.CharField(max_length=10, default="image", choices=MASK_TYPE_CHOICES, verbose_name="蒙版类型")
    mask_content = models.JSONField(default=dict, verbose_name="蒙版内容配置")
    asset = models.ForeignKey(Asset, on_delete=models.SET_NULL, null=True, blank=True,
                              verbose_name="关联资源（图片蒙版时使用）")

    class Meta:
        db_table = "mask_elements"
        verbose_name = "蒙版元素"
        verbose_name_plural = "蒙版元素"
